package com.lifeng.proxy;

import org.apache.commons.lang3.ArrayUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Created by lifeng on 2018/8/15.
 */
public class MethodProxy implements InvocationHandler {

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //如果传进来是一个已实现的具体类
        if (Object.class.equals(method.getDeclaringClass())) {
            return method.invoke(this,args);
        }else{//如果是接口
            return run(proxy, method,args);
        }
    }

    public Object run(Object proxy, Method method, Object[] args){
        System.out.println("执行真实请求");
        String methodName = method.getName();
        System.out.println("请求方法:"+methodName+",参数:"+ ArrayUtils.toString(args));
        System.out.println("参数类型:"+method.getParameterTypes()[0].getSimpleName());
        //TODO 此处可以模拟RPC 远程访问
        return null;
    }
}
